<template>
  <div>
    <el-container>
      <!-- 左侧导航栏 -->
      <el-aside>
        <navbar :menus="menus"></navbar>
      </el-aside>
      <!-- 右侧主内容区 -->
      <el-container>
        <!-- 头部 -->
        <el-header>
          <i
            :class="iconDeg"
            class="el-icon-s-fold"
            @click="handleCollapse"
          ></i>
          <el-dropdown>
            <span class="el-dropdown-link">
              下拉菜单<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>用户信息</el-dropdown-item>
              <el-dropdown-item>修改密码</el-dropdown-item>
              <el-dropdown-item>修改头像</el-dropdown-item>
              <el-dropdown-item @click.native="handleLogout"
                >注销</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-header>
        <!-- 中间内容区 -->
        <el-main><router-view></router-view></el-main>
        <!-- 底部 -->
        <el-footer>
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-bianji"></use>
          </svg>
          Footer
        </el-footer>
      </el-container>
<!-- 注销模态框  -->
<el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="30%"
  :before-close="handleClose">
  <span>确定要注销吗？</span>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="handleConfirmLogout">确 定</el-button>
  </span>
</el-dialog>

    </el-container>

  </div>
</template>

<script>
import Navbar from "./Navbar";
import routes from "../router/routes";
import {clearToken} from '../utils/auth'
export default {
  components: {
    Navbar,
  },
  data() {
    return {
      isCollape: false,
      dialogVisible:false,
    };
  },
  methods: {
    handleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
// 注销
handleLogout() {
      console.log("我的注销");
      this.dialogVisible = true;
    },
    handleConfirmLogout() {
      console.log("确定注销");
      // localStorage.removeItem("accessToken");
      // localStorage.removeItem("refreshToken");
      clearToken()
      this.$router.push('/login')
    },

    resolveForMenu(arr, parentPath = "") {
      let newArr = [];
      // 遍历传入的arr数组

      // 处理上级路径

      //判断当前对象（路由）是否具有children节点，如果有，递归处理children节点后，
      //将当前节点加入数组，，如果没有则直接将当前节点加入新数组

      //返回最后处理完成的数组

      arr.forEach((route) => {
        parentPath =
          parentPath && parentPath.lastIndexOf("/") !== parentPath.length - 1
            ? parentPath + "/"
            : parentPath;
        //console.log(route);
        //route 遍历出所有路由
        //console.log(parentPath);
        //给所有父级目录后面加上杠杠
        //父亲路径=不是空路径且结尾有个杠杠 不等于 杠杠 就执行路径加杠杠否则执行原路径
        let obj = Object.assign({}, route);
        //console.log(obj);
        //返回每个路径拼接路径
        obj.path = parentPath + obj.path;
        if (obj.children && obj.children.length > 0) {
          obj.children = this.resolveForMenu(obj.children, obj.path);
        }
        //console.log(obj.path);
        //判断是否有子路由存在则再次拼接，否则加入新数组
        //Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。
        //路由路径 = 父级路由路径+子级路由路径 循环
        //如果子级路由路径且子级路由路径长度大于0
        //console.log(obj.path);
        //console.log(obj.children);
        //则执行子级路由路径 =  （路径节点，每条路径）

        //如果某路由元数据中，设置为当前路由为隐藏，则当前路由将不作为导航菜单显示，只将children加入到菜单数组
        if (obj.meta.hidden) {
          //如果存在children属性，那么继续处理下（将所有子节点提升并加入到返回数组）反之不作处理
          if (obj.children && obj.children.length > 0) {
            obj.children.forEach((item) => {
              let tmp = Object.assign({}, item);
              newArr.push(tmp);
            });
          }
        } else {
          newArr.push(obj);
        }
      });

      return newArr;
    },
  },
  computed: {
    menus() {
      var arr = this.resolveForMenu(routes);
      return arr;
    },
    iconDeg() {
      return {
        "icon-deg": this.isCollapse ? true : "",
      };
    },
  },
};
</script>

<style scoped>
.el-header {
  background-color: #b3c0d1;
  color: #333;
/* 居中菜单栏右移 */
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.el-footer {
  background-color: #b3c0d1;
  color: #333;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
}
.el-menu {
  border-right: 0px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  min-height: calc(100vh - 120px);
}
body {
  margin: 0px !important;
  padding: 0px;
}
.icon-deg {
  transform: rotate(90deg);
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>
